package com.qiangesoft.easyexcel.util;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * 文件工具类
 *
 * @author qiangesoft
 * @date 2024-04-10
 */
public class ResourceFileUtil {

    /**
     * 项目路径
     *
     * @return
     */
    public static String getProjectPath() {
        return System.getProperty("user.dir");
    }

    /**
     * 类文件路径
     *
     * @return
     */
    public static String getClassPath() {
        return ResourceFileUtil.class.getClass().getResource("/").getPath();
    }

    /**
     * 类文件路径
     *
     * @return
     */
    public static String getClassPathByClassLoader() {
        return ClassUtils.getDefaultClassLoader().getResource("").getPath();
    }

    /**
     * 获取类路径下资源文件
     *
     * @return
     */
    public static InputStream getResourceByClassPathResource(String fileName) throws IOException {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        return classPathResource.getInputStream();
    }

    /**
     * 获取类路径下资源文件
     *
     * @return
     */
    public static InputStream getResourceByClassLoader(String fileName) {
        return ResourceFileUtil.class.getClassLoader().getResourceAsStream(fileName);
    }

    /**
     * 获取类路径下资源文件
     *
     * @return
     */
    public static InputStream getResourceByResourceLoader(String fileName) throws IOException {
        Resource resource = new DefaultResourceLoader().getResource(fileName);
        return resource.getInputStream();
    }

    public static void main(String[] args) throws IOException {
//        getResourceByResourceLoader("com/qiangesoft/easyexcel/EasyexcelApplication.class");
        System.out.println(getResourceByResourceLoader("com/qiangesoft/easyexcel/EasyexcelApplication.class"));
    }
}
